/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmium;

import java.io.InterruptedIOException;
import java.lang.reflect.Method;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.FalseExit;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides primitives related to {@link java.lang.reflect.Method}.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Methods {

    /**
     * No instance of this class.
     */
    private Methods() {
    } // end empty constructor

    /**
     * Executes {@link java.lang.reflect.Method#getClass()} method.
     * @param ctxt context
     * @param meth object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_meth_get_declaring_class(final CodeRunner ctxt,
                                                         final Value meth)
        throws Fail.Exception {
        try {
            final Method m = (Method) meth.asBlock().asCustom();
            return Cadmium.createObject(m.getDeclaringClass());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_meth_get_declaring_class(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.reflect.Method#getExceptionTypes()} method.
     * @param ctxt context
     * @param meth object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_meth_get_exception_types(final CodeRunner ctxt,
                                                         final Value meth)
        throws Fail.Exception {
        try {
            final Method m = (Method) meth.asBlock().asCustom();
            return Cadmium.createObjectList(m.getExceptionTypes());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_meth_get_exception_types(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.reflect.Method#getModifiers()} method.
     * @param ctxt context
     * @param meth object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_meth_get_modifiers(final CodeRunner ctxt,
                                                   final Value meth)
        throws Fail.Exception {
        try {
            final Method m = (Method) meth.asBlock().asCustom();
            return Cadmium.createModifiersList(m.getModifiers());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_meth_get_modifiers(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.reflect.Method#getName()} method.
     * @param ctxt context
     * @param meth object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_meth_get_name(final CodeRunner ctxt,
                                              final Value meth)
        throws Fail.Exception {
        try {
            final Method m = (Method) meth.asBlock().asCustom();
            return Value.createFromBlock(Block.createString(m.getName()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_meth_get_name(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.reflect.Method#getParameterTypes()} method.
     * @param ctxt context
     * @param meth object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_meth_get_parameter_types(final CodeRunner ctxt,
                                                         final Value meth)
        throws Fail.Exception {
        try {
            final Method m = (Method) meth.asBlock().asCustom();
            return Cadmium.encodeTypeList(m.getParameterTypes());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_meth_get_parameter_types(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.reflect.Method#getReturnType()} method.
     * @param ctxt context
     * @param meth object to execute method on
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_meth_get_return_type(final CodeRunner ctxt,
                                                     final Value meth)
        throws Fail.Exception {
        try {
            final Method m = (Method) meth.asBlock().asCustom();
            return Cadmium.encodeType(m.getReturnType());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_meth_get_return_type(CodeRunner, Value)'

    /**
     * Executes {@link java.lang.reflect.Method#invoke(java.lang.Object, java.lang.Object[])} method.
     * @param ctxt context
     * @param meth object to execute method on
     * @param obj parameter for method execution
     * @param params parameter for method execution
     * @return result of method execution
     * @throws Fail.Exception if Java method throws an exception
     */
    @Primitive
    public static Value cadmium_meth_invoke(final CodeRunner ctxt,
                                            final Value meth,
                                            final Value obj,
                                            final Value params)
        throws Fail.Exception, FalseExit {
        try {
            final Method m = (Method) meth.asBlock().asCustom();
            final Object o = obj.asBlock().asCustom();
            final Object[] args = Cadmium.decodeObjectList(params);
            return Cadmium.encodeObject(m.invoke(o, args));
        } catch (final Exception e) {
            final Throwable cause = e.getCause();
            if ((cause instanceof InterruptedException)
                || (cause instanceof InterruptedIOException)) {
                final FalseExit fe =
                    FalseExit.createFromContext(ctxt.getContext());
                fe.fillInStackTrace();
                throw fe;
            } // end if
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_meth_invoke(CodeRunner, Value, Value, Value)'

    /**
     * Casts an object to a method.
     * @param ctxt context
     * @param obj object to cast
     * @return object as method
     * @throws Fail.Exception if cast fails
     */
    @Primitive
    public static Value cadmium_meth_as_meth(final CodeRunner ctxt,
                                             final Value obj)
        throws Fail.Exception {
        try {
            final Block objBlock = obj.asBlock();
            objBlock.setCustom((Method) objBlock.asCustom());
            return obj;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmium_meth_as_meth(CodeRunner, Value)'

} // end class 'Methods'
